﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Logistics.Commn
{
    public class EmailHelper
    {
        /// <summary>
        /// 重置密码(25端口)
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="mail">邮箱地址</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public static string SendMailResetPwd(string userName, string mail, string code)
        {
            var result = string.Empty;
            try
            {
                var smtp = new System.Net.Mail.SmtpClient()
                {
                    Host = "smtp.163.com",
                    Port = 25,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new NetworkCredential("LHWTG_LCY@163.com", "NDUOLXFMWYKMCCJT")
                };
                var mm = new MailMessage();
                mm.From = new MailAddress("LHWTG_LCY@163.com", "TMS智慧物联客服");
                mm.To.Add(new MailAddress(mail, "TMS智慧物联客服"));
                mm.Subject = "重置密码 - TMS智慧物联客服";
                mm.SubjectEncoding = Encoding.UTF8;
                mm.IsBodyHtml = true;
                mm.BodyEncoding = Encoding.UTF8;
                var sbEmail = new StringBuilder();
                sbEmail.Append("<div style='text-indent:24px;padding-bottom:15px;'> 您好，您正在重置登录用户名为 <span style='font-weight:bolder'>" + userName + "</span> 的TMS账户密码。</div>");
                sbEmail.Append("<div style='text-indent:24px;padding-bottom:15px;'> 您的验证码为: <span style='color:red'>" + code + "</span> ，有效时间为1分钟，请在有效时间内进行验证。</div>");
                sbEmail.Append("<div style='text-indent:24px;padding-bottom:35px;'>【警告】：请不要把验证码泄露给其他人，因个人泄露验证码造成的损失，由个人负责！</div>");
                mm.Body = sbEmail.ToString();
                smtp.SendAsync(mm, null);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
        }
    }
}
